<?php
	session_start();

	include("std_dbs.php");
                      
$sortOrder = $_POST['sortOrder'];

?>	

<b>Leave the search box blank to view all logs!</b><br />   
<form name="sortReviews" method="post" action="admin.php#Log">
User Name: <input type="text" name="username" /> Sort By: <select name='sortOrder'>
	<option value='LOGDATE DESC' selected>Date</option>
	<option value='ACTION ASC'>Action</option>
	<option value='IPADDRESS DESC'>IP Address</td>
	</select>    
	<input type='submit' value='Sort' name='sort'><br /><br />                    
	</form>
	<font color='red'><?php echo $message; ?></font>
		<?php
			$sortOrder = addslashes($_POST['sortOrder']);
			$username = addslashes($_POST['username']);

			//build query
                	$query = "SELECT USERNAME, LOGID, LOGDATE, ACTION, IPADDRESS FROM USERLOG INNER JOIN USERS ON UserLog.USERID 
= Users.USERID WHERE USERNAME LIKE '%" . $username . "%' ORDER BY $sortOrder";

                	//Prepare and execute query
                	$stid = oci_parse($connect, $query);
                	$r = oci_execute($stid);

			if (!$r) 
			{
/*
			    	$e = oci_error($stid);  // For oci_execute errors pass the statement handle
			    	print htmlentities($e['message']);
			    	print "\n<pre>\n";
    				print htmlentities($e['sqltext']);
				echo $e['message'];
    				printf("\n%".($e['offset']+1)."s", "^");
    				print  "\n</pre>\n";
*/
				$message = "There was an error while grabbing the logs!";
			}
			else
			{
				//Display results
				echo "<table width='95%' id='logTable'><tr style='font-weight:bold'>
					<td>User Name</td><td>Log ID</td><td>Log Date:</td><td>Action:</td><td>IP Address</td></tr>";
			
				while(($row = oci_fetch_array($stid, OCI_ASSOC)))
				{
					//pull info from database
					$username = $row['USERNAME'];
					$logid = $row['LOGID'];
					$logdate = $row['LOGDATE'];
					$action = $row['ACTION'];
					$ipaddress = $row['IPADDRESS'];				

					//echo info into a table
					echo "<tr><td>$username</td><td>$logid</td><td>$logdate</td><td>$action</td><td>$ipaddress</td></tr>";
				}
			
				echo "</table>";
			}			
		?>	
	
